package cc.wsyw126.java.nowcoder.sword_offer;

import java.util.Stack;

/**
 * Created by Administrator on 2017/4/8.
 */
public class JudgeStackOrder {
    private Stack<Integer> stack = new Stack<>();
    private int i = 0;

    public boolean IsPopOrder(int[] pushA, int[] popA) {
        boolean result = false;
        for (int j = 0; j < pushA.length; j++) {
            stack.push(pushA[j]);
            while (!stack.isEmpty() && stack.peek() == popA[i] && i < popA.length) {
                stack.pop();
                i++;
            }
        }
        if (stack.isEmpty() && i == popA.length) {
            result = true;
        }
        i = 0;
        return result;
    }

    public static void main(String[] args) {
        JudgeStackOrder judgeStackOrder = new JudgeStackOrder();
        int[] pushA = {1, 2, 3, 4, 5};
        int[] popA = {4, 5, 3, 2, 1};
        boolean b = judgeStackOrder.IsPopOrder(pushA, popA);
        System.out.println("b = " + b);
        int[] popB = {4, 3, 5, 1, 2};
        b = judgeStackOrder.IsPopOrder(pushA, popB);
        System.out.println("b = " + b);
    }
}
